import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/home/home'
import Index from './views/index/index'
import Movie from './views/movie/movie'
import Cinema from './views/cinema/cinema'
import User from './views/user/user'
import MeituanLogin from './components/meituanLogin/meituanLogin'
import PhoneLogin from './components/phoneLogin/phoneLogin'
import Register from './components/register/register'
import PhoneNumber from './components/phoneNum/phoneNumber'
import Code from './components/code/code'
import SetPassword from './components/setPassword/setPassword'
import UserInfo from './views/userInfo/userInfo'
import city from  './views/city/city'
import notFound from  './views/notFound/notFound'
import cinemaSearch from './views/cinemaSearch/search'
import cinemaList from './views/cinemalist/cinemalist'

Vue.use(Router)

let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: {name: 'home'},
    },
    {
      path: '/user',
      name: 'user',
      component: User,
      redirect: {path: '/user/meituanLogin'},
      children: [
        {
          path: '/user/meituanLogin',
          component: MeituanLogin,
        },
        {
          path: '/user/phoneLogin',
          component: PhoneLogin
        }
      ]
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      redirect: {path: '/register/phoneNumber'},
      children: [
        {
          path: '/register/phoneNumber',
          component: PhoneNumber
        },
        {
          path: '/register/code',
          component: Code
        },
        {
          path: '/register/setPassword',
          component: SetPassword
        },
      ]
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      redirect: {name: 'index'},
      meta: { requiresAuth: true },
      children: [
        {
          path: '/home/userInfo',
          name: 'userInfo',
          component: UserInfo
        },
        {
          path: '/home/index',
          name: 'index',
          component: Index,
          redirect: {path: '/home/index/1'},
          children: [
            {
              path: ':type',
              name: 'movie',
              component: Movie
            }
          ]
        },
        {
          path: '/home/cinema',
          name: 'cinema',
          component: Cinema
        }
      ]
    },
    {
      path:'/search',
      name:'cinemaSearch',
      component:cinemaSearch
    },
    {
      path:'/cinemalist',
      name:'cinemaList',
      component:cinemaList
    },
    {
      path:"/city",
      component:city
    },
    {
      path:'**',
      component:notFound
    }
  ]
})

router.beforeEach((to, from, next) => {
  localStorage.setItem('path',from.path)
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!sessionStorage.getItem('token')) {
      next({
        path: '/user',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router
